#include "QtGroupAnimationEffect.h"


QtGroupAnimationEffect::QtGroupAnimationEffect()
{
}

QtGroupAnimationEffect::QtGroupAnimationEffect(const QtGroupAnimationEffect& rhs)
{
	std::list<QtAnimationEffect*>::const_iterator it;

	for(it = rhs.effects.begin(); it != rhs.effects.end(); ++it)
	{
		QtAnimationEffect *effect = *it;

		effects.push_back( effect->Clone() );
	}
}

QtGroupAnimationEffect::~QtGroupAnimationEffect()
{
	std::list<QtAnimationEffect*>::iterator it;

	for(it = effects.begin(); it != effects.end(); ++it)
	{
		delete (*it);
	}

	effects.clear();
}

void QtGroupAnimationEffect::SetDuration(unsigned int newDuration)
{
	std::list<QtAnimationEffect*>::iterator it;

	for(it = effects.begin(); it != effects.end(); ++it)
	{
		(*it)->SetDuration(newDuration);
	}
}

void QtGroupAnimationEffect::Start()
{
	std::list<QtAnimationEffect*>::iterator it;

	for(it = effects.begin(); it != effects.end(); ++it)
	{
		(*it)->Start();
	}

	EmitStarted();
}

void QtGroupAnimationEffect::AddEffect(QtAnimationEffect *effect)
{
	QtAnimationEffect *clone = effect->Clone();

	effects.push_back( clone );

	QObject::connect(clone, SIGNAL(Finished(QtAnimationEffect*)), this, SLOT(EffectFinished(QtAnimationEffect*)));
}


void QtGroupAnimationEffect::EffectFinished(QtAnimationEffect *effect)
{
	std::list<QtAnimationEffect*>::iterator it;

	for(it = effects.begin(); it != effects.end(); ++it)
	{
		if ( effect == (*it) )
		{
			effects.erase(it);
			delete effect;
			break;
		}
	}

	if (effects.size() == 0)
	{
		EmitFinished();
	}
}


QtAnimationEffect* QtGroupAnimationEffect::Clone() const
{
	return new QtGroupAnimationEffect(*this);
}


